package com.dkt.login;

import com.dkt.common.SysConst;
import com.dkt.common.UserCache;
import com.dkt.entity.UserDoctorInfo;
import com.dkt.entity.UserOper;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by litai on 2017/6/14.
 */
@Service
public class T0001ServiceImpl implements T0001Service {

    private static final Logger LOG = LoggerFactory.getLogger(T0001Service.class);

    @Autowired
    private T0001Dao t0001Dao;
    @Autowired
    private P0013Service p0013Service;

    @Override
    public T0001ResponseBean findByUp(String userName, String passwd) throws IllegalAccessException {
        UserOper userOper = t0001Dao.findByUp(userName, Hashing.md5().newHasher()
                .putString(passwd, Charsets.UTF_8).hash().toString());
        T0001ResponseBean trb = new T0001ResponseBean();
        if (null == userOper)
            throw new IllegalArgumentException("用户不存在");
        if(SysConst.OPER_STATUS_LOCKED.equals(userOper.getStatus()))
            throw new IllegalAccessException("账号被锁定");
        trb.setRoles(userOper.getRoles());
        switch(userOper.getRoles()) {
            case SysConst.ROLE_DOCTOR:
                UserDoctorInfo doctor = p0013Service.findOne(userOper.getOperId());
                if (null == doctor) {
                    LOG.warn("医生表中不存在登录信息表中的医生信息: id={}", userOper.getOperId());
                } else {
                    trb.setDoctorId(userOper.getOperId());
                    trb.setDoctorPhoto(doctor.getDoctorHeadIcon());
                }
                break;
            default:
                // TODO leave for other case
        }

        trb.setTicket(UserCache.getInstance().save(userOper.getAccount()));
        String casuuid = UUID.randomUUID().toString().replace("-", "");
        trb.setCasUuid(casuuid);
        return trb;
    }
}
